
/**
 * Erstellt http requests
 */
#include <string.h>
#include <errno.h>
#include <ogcsys.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //usleep
#include <network.h>

char* getmakeeasy(char *url)  //dont vorgot to free!!!
{	
	//free(header);
	//Check if the url starts with "http://", if not it is not considered a valid url
    if (strncmp(url, "http://", strlen("http://")) != 0) {
        //printf("URL '%s' doesn't start with 'http://'\n", url);
        return "";
    }

    //Locate the path part of the url by searching for '/' past "http://"
    char *path = strchr(url + strlen("http://"), '/');

    //At the very least the url has to end with '/', ending with just a domain is invalid
    /*if (path == NULL) {
        //printf("URL '%s' has no PATH part\n", url);
        return "";
    }*/

    //Extract the domain part out of the url
    int domainlength = path - url - strlen("http://");

    if (domainlength == 0) {
        //printf("No domain part in URL '%s'\n", url);
        return "";
    }
    char domain[domainlength + 1];
    strlcpy(domain, url + strlen("http://"), domainlength+1);
	if(path == NULL)
	{
		char* headerformat = "GET / HTTP/1.1\r\nHost: %s\r\nUser-Agent: ichflynetrequest\r\nConnection: close\r\n\r\n";
		char* header = malloc(strlen(headerformat) + strlen(path) + strlen(domain));
		sprintf(header, headerformat, domain);
		return header;
	}
	else
	{
		char* headerformat = "GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n";
		char* header = malloc(strlen(headerformat) + strlen(path) + strlen(domain) + strlen(domain));
		sprintf(header, headerformat, path, domain);
		return header;
	}
}